package window;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.Color;

import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import bean.JavaBean;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;
import java.awt.event.ActionEvent;
import dao.TeacherDao;
import entity.DbToExcel;
import entity.ExcelToDb;


import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

@SuppressWarnings("serial")
class MyTable extends JTable{
	public boolean isCellEditable(int row,int column) {
		return false;
	}
}

public class TeacherHomepage {
	String id="";
	public JFrame teacherHomepageFrame;

	/**
	 * Launch the application.
	 */

	
	/**
	 * Create the application.
	 */
	public TeacherHomepage(String id) {
		this.id=id;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		teacherHomepageFrame = new JFrame();
	
		
		teacherHomepageFrame.setResizable(false);
		teacherHomepageFrame.setBounds(-7, 0, 1380, 736);
		teacherHomepageFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		teacherHomepageFrame.getContentPane().setLayout(null);
		teacherHomepageFrame.setTitle("教师信息管理系统");
		ImageIcon icon=new ImageIcon("D:\\eclipse\\workspace\\TeacherInformationManagementSystem\\src\\window\\1.png");
		teacherHomepageFrame.setIconImage(icon.getImage());
		JLabel lblNewLabel = new JLabel("教师信息管理系统");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setForeground(Color.DARK_GRAY);
		lblNewLabel.setFont(new Font("宋体", Font.BOLD, 30));
		lblNewLabel.setBounds(0, 50, 1364, 31);
		teacherHomepageFrame.getContentPane().add(lblNewLabel);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 1364, 23);
		teacherHomepageFrame.getContentPane().add(menuBar);
		
		JMenu mnNewMenu = new JMenu("操作(ALT+1)");
		mnNewMenu.setHorizontalAlignment(SwingConstants.CENTER);
		mnNewMenu.setFont(new Font("幼圆", Font.PLAIN, 15));
		mnNewMenu.setForeground(Color.DARK_GRAY);
		mnNewMenu.setMnemonic('1');
		menuBar.add(mnNewMenu);
		
		JMenuItem mntma = new JMenuItem("录入(Q)");
		mntma.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Addpage ap=new Addpage(id);
				ap.addpageFrame.setVisible(true);
				teacherHomepageFrame.dispose();
			}
		});
		mntma.setFont(new Font("幼圆", Font.PLAIN, 13));
		mntma.setForeground(Color.DARK_GRAY);
		mntma.setMnemonic('Q');
		mnNewMenu.add(mntma);
		
		JMenuItem mntmu = new JMenuItem("修改(W)");
		mntmu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				IUpdatepage up=new IUpdatepage(id);
				up.iUpdatepageFrame.setVisible(true);
				teacherHomepageFrame.dispose();
			}
		});
		mntmu.setFont(new Font("幼圆", Font.PLAIN, 13));
		mntmu.setForeground(Color.DARK_GRAY);
		mntmu.setMnemonic('W');
		mnNewMenu.add(mntmu);
		
		
		JMenuItem mntmr = new JMenuItem("删除(R)");
		mntmr.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Deletepage dp=new Deletepage(id);
				dp.deletepageFrame.setVisible(true);
				teacherHomepageFrame.dispose();
			}
		});
		
		JMenuItem mntms = new JMenuItem("查询(E)");
		mntms.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ISelectpage sp=new ISelectpage(id);
				sp.iSelectpageFrame.setVisible(true);
				teacherHomepageFrame.dispose();
			}
		});
		mntms.setFont(new Font("幼圆", Font.PLAIN, 13));
		mntms.setForeground(Color.DARK_GRAY);
		mntms.setMnemonic('E');
		mnNewMenu.add(mntms);
		
		mntmr.setMnemonic('R');
		mntmr.setForeground(Color.DARK_GRAY);
		mntmr.setFont(new Font("幼圆", Font.PLAIN, 13));
		mnNewMenu.add(mntmr);
		
		JMenuItem mntme = new JMenuItem("退出登录(Y)");
		mntme.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Login lg=new Login();
				lg.LoginFrame.setVisible(true);
				teacherHomepageFrame.dispose();
			}
		});
		
		JMenuItem mntmm = new JMenuItem("统计(T)");
		mntmm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				IStatisticspage isp=new IStatisticspage(id);
				isp.iStatisticspageFrame.setVisible(true);
				teacherHomepageFrame.dispose();
			}
		});
		mntmm.setFont(new Font("幼圆", Font.PLAIN, 13));
		mntmm.setForeground(Color.DARK_GRAY);
		mntmm.setMnemonic('T');
		mnNewMenu.add(mntmm);
		
		mntme.setMnemonic('Y');
		mntme.setForeground(Color.DARK_GRAY);
		mntme.setFont(new Font("幼圆", Font.PLAIN, 13));
		mnNewMenu.add(mntme);
		
		
		
		JLabel lblNewLabel_1 = new JLabel("欢迎你，亲爱的");
		lblNewLabel_1.setForeground(Color.DARK_GRAY);
		lblNewLabel_1.setFont(new Font("幼圆", Font.PLAIN, 12));
		lblNewLabel_1.setBounds(10, 37, 90, 15);
		teacherHomepageFrame.getContentPane().add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("New label");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_2.setText(id);
		lblNewLabel_2.setForeground(Color.DARK_GRAY);
		lblNewLabel_2.setFont(new Font("幼圆", Font.BOLD, 13));
		lblNewLabel_2.setBounds(98, 36, 75, 15);
		teacherHomepageFrame.getContentPane().add(lblNewLabel_2);
		
	//测试	
	/*	String[][] tableData =
			  {
			    new String[]{"李清照" , "29" , "女"},
			    new String[]{"苏格拉底", "56" , "男"},
			    new String[]{"李白", "35" , "男"},
			    new String[]{"弄玉", "18", "女"},
			    new String[]{"虎头" , "2" , "男"}
			  };
		String[] rowdata= {"工号","姓名","年龄","职称","所在系","课程","联系电话","联系地址"};
		table = new JTable(tableData, rowdata);
		table.setForeground(Color.DARK_GRAY);
		table.setFont(new Font("幼圆", Font.PLAIN, 13));
		JScrollPane JSP= new JScrollPane(table);
		JSP.setBounds(40, 144, 900, 392);
		TeacherHomepageFrame.getContentPane().add(JSP);*/
		
		  MyTable table = new MyTable();
		  table.setForeground(Color.DARK_GRAY);
		  table.setFont(new Font("幼圆", Font.PLAIN, 13));
		  
		 /* table.setEnabled(false);*/
		 
		  String[] tableHeads = new String[]{"工号","姓名","年龄","职称","所在系","教授课程","联系电话","联系地址"};
		  DefaultTableModel dtm = (DefaultTableModel)table.getModel();
		  dtm.setColumnIdentifiers(tableHeads);
		  
		  JButton btnNewButton = new JButton("批量导入(I)");
		  JButton btne = new JButton("批量导出(E)");
		  btne.addActionListener(new ActionListener() {
		  	public void actionPerformed(ActionEvent arg0) {
		  		JFileChooser chooser = new JFileChooser();             
		  		chooser.setMultiSelectionEnabled(false);           
		  		chooser.showSaveDialog(btne);       
				try {
					DbToExcel.dbToExcel(table, chooser.getSelectedFile());
					JOptionPane.showMessageDialog(null, "导出成功！", "success！", JOptionPane.PLAIN_MESSAGE);
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					JOptionPane.showMessageDialog(null, "导入失败！", "error！", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}

		  	}
		  });
		  table.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if(e.getKeyCode()==KeyEvent.VK_I)
						btnNewButton.doClick();
					if(e.getKeyCode()==KeyEvent.VK_E)
						btne.doClick();
					/*System.out.println("I");*/
				}
			});
		  table.addMouseListener(new MouseAdapter() {
			  	@Override
			  	public void mousePressed(MouseEvent arg0) {
			  		/*System.out.println(dtm.getValueAt(table.getSelectedRow(), table.getSelectedColumn()));*/
			  		/*JOptionPane.showMessageDialog(null, dtm.getValueAt(table.getSelectedRow(), table.getSelectedColumn()), "获取单元格值!", JOptionPane.PLAIN_MESSAGE);*/
			  		Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
			  		Transferable tf=new StringSelection(String.valueOf(dtm.getValueAt(table.getSelectedRow(), table.getSelectedColumn())));
			  		cb.setContents(tf, null);
			  	}
			  });

		  List<JavaBean> l=TeacherDao.readList();
		  for(int i=0;i<l.size();i++) 
		  { 
		      Object[]rowData={l.get(i).getId(),l.get(i).getName(),
		      l.get(i).getAge(),l.get(i).getTitle(),
		      l.get(i).getDepartment(),l.get(i).getCourse(),
		      l.get(i).getPhone(),l.get(i).getAddress(),
		      };
		      dtm.addRow(rowData);
		} 
		  
		  
		  
		  table.getColumnModel().getColumn(7).setPreferredWidth(370);
		  DefaultTableCellRenderer r = new DefaultTableCellRenderer();
		  r.setHorizontalAlignment(JLabel.CENTER);
		  table.setDefaultRenderer(Object.class, r);
		  
		  JScrollPane JSP= new JScrollPane(table);
		 
		  JSP.setViewportBorder(new LineBorder(Color.GRAY));
		  JSP.setBounds(10, 103, 1344, 495);
		  teacherHomepageFrame.getContentPane().add(JSP);
		  
		  
		    btnNewButton.addActionListener(new ActionListener() {
		  	public void actionPerformed(ActionEvent arg0) {
		  		JFileChooser chooser = new JFileChooser();             //设置选择器
		  		chooser.setMultiSelectionEnabled(false);             //设为多选
		  		chooser.showOpenDialog(btnNewButton);        //是否打开文件选择框
		  		String filepath = chooser.getSelectedFile().getAbsolutePath();      //获取绝对路径
		  		/*System.out.println(filepath);*/
		  		ExcelToDb.excelToDb(filepath);
		  		if(ExcelToDb.excelToDb(filepath)) {
		  			JOptionPane.showMessageDialog(null, "导入成功！", "succe！", JOptionPane.PLAIN_MESSAGE);
		  			TeacherHomepage th=new TeacherHomepage(id);
		  			teacherHomepageFrame.dispose();
		  			th.teacherHomepageFrame.setVisible(true);
		  		}
		  		else
		  			JOptionPane.showMessageDialog(null, "导入失败！", "error！", JOptionPane.ERROR_MESSAGE);
		  	}
		  });
		  btnNewButton.setFont(new Font("幼圆", Font.BOLD, 16));
		  btnNewButton.setForeground(Color.DARK_GRAY);
		  btnNewButton.setBounds(45, 628, 143, 37);
		  teacherHomepageFrame.getContentPane().add(btnNewButton);
		  
		 
		  btne.setForeground(Color.DARK_GRAY);
		  btne.setFont(new Font("幼圆", Font.BOLD, 16));
		  btne.setBounds(215, 628, 143, 37);
		  teacherHomepageFrame.getContentPane().add(btne);
		  
		  JLabel label = new JLabel("统计结果：共");
		  label.setForeground(Color.DARK_GRAY);
		  label.setFont(new Font("幼圆", Font.BOLD, 18));
		  label.setBounds(957, 633, 128, 23);
		  teacherHomepageFrame.getContentPane().add(label);
		  
		  JLabel label_1 = new JLabel("条");
		  label_1.setForeground(Color.DARK_GRAY);
		  label_1.setFont(new Font("幼圆", Font.BOLD, 18));
		  label_1.setBounds(1172, 633, 100, 23);
		  teacherHomepageFrame.getContentPane().add(label_1);
		  
		  JLabel label_2 = new JLabel("0");
		  label_2.setHorizontalAlignment(SwingConstants.CENTER);
		  label_2.setText(String.valueOf(l.size()));
		  label_2.setForeground(Color.DARK_GRAY);
		  label_2.setFont(new Font("幼圆", Font.BOLD | Font.ITALIC, 18));
		  label_2.setBounds(1095, 633, 67, 23);
		  teacherHomepageFrame.getContentPane().add(label_2);
		  
		  JLabel label_3 = new JLabel("，现在是：");
		  label_3.setForeground(Color.DARK_GRAY);
		  label_3.setFont(new Font("幼圆", Font.PLAIN, 12));
		  label_3.setBounds(175, 36, 60, 15);
		  teacherHomepageFrame.getContentPane().add(label_3);
		  
		 /* JLabel label_4 = new JLabel("");*/
		/*  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式*/
		  /*System.out.println(df.format(new Date()));// new Date()为获取当前系统时间*/
		  /*label_4.setText(df.format(new Date()));*/
		 
		  
                           


		 /* label_4.setForeground(Color.DARK_GRAY);
		  label_4.setFont(new Font("幼圆", Font.PLAIN, 12));
		  label_4.setBounds(233, 36, 150, 15);*/
		  MyTime t = new MyTime();
		  new Thread(t).start();//启动线程
		  /*teacherHomepageFrame.getContentPane().add(label_4);*/
		
		
	/*	//排版不好看
		JList<JavaBean> list = new JList<JavaBean>();
		List<JavaBean> l=TeacherDao.readList();
		DefaultListModel dlm = new DefaultListModel();
		for(JavaBean j:l)
		{
			dlm.addElement(new String(String.valueOf(j.getId())+"     "+j.getName()+"          "+j.getAge()+"             "+j.getTitle()+"             "+j.getDepartment()+"             "+j.getCourse()+"        "+j.getPhone()+"           "+j.getAddress()));
			list.setModel(dlm);
		}
		list.setBounds(40, 156, 900, 370);
		TeacherHomepageFrame.getContentPane().add(list);*/
	
	}
	
	//这里需要继承JFrame并实现Runnable接口，可以理解为定制的过程。其中继承JFrame
	class MyTime extends JFrame implements Runnable {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		JLabel MyJLabel = new JLabel();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
		
		//构造函数：当new一个对象的时候，调用构造函数完成对象的初始化。
		//效果：当我们new这个定制的MyTime时，会给我们创建一个JLabel，并赋予一些属性。
		public MyTime() {		
			MyJLabel = new JLabel(sdf.format(new Date()));
			
			//赋予JLabel一些属性值。
			MyJLabel.setForeground(Color.DARK_GRAY);
			MyJLabel.setFont(new Font("幼圆", Font.PLAIN, 12));
			MyJLabel.setBounds(233, 36, 250, 15);
			
			//将该JLabel add进总JFrame中
			teacherHomepageFrame.getContentPane().add(MyJLabel);
			}

		//实现Runnable接口，重写其中的run方法，创建线程。其中run()方法的方法体是线程执行体。
		@Override
		public void run() {
			// TODO Auto-generated method stub
			while (true) {
				try {
				Thread.sleep(100);//Thread.Sleep()方法用于将当前线程休眠一定时间，单位为毫秒。这里为每100毫秒休眠一次线程。
				MyJLabel.setText(sdf.format(new Date()));//把当前的系统时间赋予到我们自定义的JLabel中
				} catch (InterruptedException e) {
				e.printStackTrace();//抛出异常
				}
				}
		}
	}
}
